package user

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
	"weekly-exam-3/config"
	"weekly-exam-3/internal/model/dao"
	"weekly-exam-3/tools"
)

type Mobile struct {
	Name     string  `form:"name" json:"name" xml:"name"  binding:"required"`
	Color    string  `form:"color" json:"color" xml:"color"  binding:"required"`
	Size     int     `form:"size" json:"size" xml:"size"  binding:"required"`
	BuyStyle string  `form:"buyStyle" json:"buyStyle" xml:"buyStyle"  binding:"required"`
	Price    float64 `form:"price" json:"price" xml:"price"  binding:"required"`
}

func Add(c *gin.Context) {
	var json Mobile
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	table := dao.MobileTable{
		Name:     json.Name,
		Color:    json.Color,
		Size:     json.Size,
		BuyStyle: json.BuyStyle,
		Price:    json.Price,
	}
	config.DB.Create(&table)
	rabbitmq := tools.NewRabbitMQSimple("Price")
	rabbitmq.PublishSimple(table.Name)
	time.Sleep(1 * time.Second)

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "估价成功",
	})
}
